package com.tanhua.admin.service;

/**
 * @Author: cl
 * @Date: 2022/5/13 - 05 - 13
 * @Description:
 */

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import net.sf.jsqlparser.schema.Column;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 用户管理模块
 */
@Service
public class ManagerService {

    //用户信息服务
    @DubboReference
    private UserInfoApi userInfoApi;

    //小视频服务
    @DubboReference
    private VideoApi videoApi;

    //动态服务
    @DubboReference
    private MovementApi movementApi;

    //redis
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 获取所有注册用户
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getUsers(Integer page, Integer pagesize) {

        //1、远程调用用户信息服务查询所有用户(利用myBatisPlus的分页查询)
        IPage<UserInfo> iPage = userInfoApi.findAll(page, pagesize);

        List<UserInfo> userInfos = iPage.getRecords();
        for (UserInfo userInfo : userInfos) {
            String s = redisTemplate.opsForValue().get(Constants.FREEZE_USER + userInfo.getId());
            if (!StringUtils.isEmpty(s)) {
                userInfo.setUserStatus("2");
            }
        }
        //2、封装pageResult对象
        PageResult pageResult = new PageResult(page, pagesize, iPage.getTotal(), userInfos);
        //3、返回结果
        return pageResult;
    }

    /**
     * 根据用户的id查询用户的详情
     *
     * @param userId
     * @return
     */
    public UserInfo getByUserId(Long userId) {
        //调用服务根据id查询用户详情
        UserInfo userInfo = userInfoApi.findById(userId);
        String s = redisTemplate.opsForValue().get(Constants.FREEZE_USER + userInfo.getId());
        if (!StringUtils.isEmpty(s)) {
            userInfo.setUserStatus("2");
        }
        return userInfo;
    }

    /**
     * 根据用户的id查询用户所有小视频
     *
     * @param page
     * @param pagesize
     * @param uid
     * @return
     */
    public PageResult getVideosByUid(Integer page, Integer pagesize, Long uid) {
        //调用服务根据用户id查询所有小视频
        PageResult pageResult = videoApi.getVideosByUid(page, pagesize, uid);
        return pageResult;
    }

    /**
     * 根据用户的id或审核状态分页查询用户所有动态
     *
     * @param page
     * @param pagesize
     * @param uid
     * @return
     */
    public PageResult getMovementByUidAndState(Integer page, Integer pagesize, Long uid, String state) {

        //1、调用动态服务查询所有动态
        PageResult pageResult = movementApi.getMovementByUidAndState(page, pagesize, uid, state);
        //2、判断是否有动态
        List<Movement> movements = (List<Movement>) pageResult.getItems();
        if (CollUtil.isEmpty(movements)) {
            //没有数据直接返回
            return pageResult;
        }
        //3、获取用户的id查询用户的详情
        List<Long> ids = CollUtil.getFieldValues(movements, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.selectByIds(ids, new RecommendUserDto());

        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : movements) {
            UserInfo userInfo = map.get(movement.getUserId());
            if (userInfo != null) {
                MovementsVo init = MovementsVo.init(userInfo, movement);
                vos.add(init);
            }
        }

        //把返回对象的集合属性变为vo集合
        pageResult.setItems(vos);
        return pageResult;
    }

    /**
     * 冻结用户
     *
     * @param params
     * @return
     */
    public Map freeze(Map params) {

        //1、获取参数
        String freezingTime = (String) params.get("freezingTime");//冻结时间
        Integer userId = (Integer) params.get("userId");//冻结用户id
        //2、判断参数
        int days = 0;
        if (Integer.parseInt(freezingTime) == 1) {
            days = 3;
        }
        if (Integer.parseInt(freezingTime) == 2) {
            days = 7;
        }
        if (Integer.parseInt(freezingTime) == 3) {
            days = -1;
        }
        //3、将冻结信息放入redis
        String v = JSON.toJSONString(params);
        redisTemplate.opsForValue().set(Constants.FREEZE_USER + userId, v, days, TimeUnit.DAYS);

        Map map = new HashMap();
        map.put("message", "冻结成功");
        return map;
    }

    /**
     * 用户解冻
     *
     * @param params
     * @return
     */
    public Map userUnfreeze(Map params) {
        Integer userId = (Integer) params.get("userId");
        redisTemplate.delete(Constants.FREEZE_USER+userId);
        Map map = new HashMap();
        map.put("message","解冻成功");
        return map;
    }
}
